package cn.itcast.goods.user.servlet;

import cn.itcast.commons.CommonUtils;
import cn.itcast.goods.user.domain.User;
import cn.itcast.goods.user.service.UserService;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 *用户界面逻辑
 */
public class UserServlet extends Base {
    HttpSession session=null;
    UserService service=new UserService();
    /**
     * 检验用户邮件是否存在
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    public String AJAXcheckEmail(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //得到验证码的参数

        String code=req.getParameter("email");
        //通过servlet得到校验结果
        boolean boo=service.AJAXcheckEmail(code);
        //向客户端返回数据
        resp.getWriter().print(boo);
        return null;
    }
    /**
     * 检验用户名是否存在
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    public String AJAXcheckUserName(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        resp.setContentType("text/html;charset=UTF-8");
        //得到验证码的参数
        String code=req.getParameter("loginname");
        //通过servlet得到校验结果
        boolean boo=service.AJAXcheckUserName(code);
        //向客户端返回数据
        resp.getWriter().print(boo);
        return null;
    }
    /**
     * 检验验证码是否正确
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    public String AJAXcheckVerifyCode(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //得到输入框文本
        String newCode=req.getParameter("verifyCode");
        System.out.println(newCode);
        //得到图片上的验证码
        String oldCode=(String)req.getSession().getAttribute("vCode");
        //进行比较
        boolean boo=newCode.equalsIgnoreCase(oldCode);
        //向客户端返回结果
        resp.getWriter().print(boo);
        //
        return null;
    }
    /**
     * 校验验证码
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    public String activation(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //得到输入框文本
        return null;
    } /**
     * 添加用户
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    public String addUser(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //得到输入框文本,封装到User中
        User userForm= CommonUtils.toBean(req.getParameterMap(),User.class);
        //校验
       /* Map<String,String> map=validata(userForm,req.getSession());
        System.out.println(map.size());
         if(map.size()>0)
         {
             req.setAttribute("form",userForm);
             req.setAttribute("errors",map);
             return "f:/jsps/user/regist.jsp";
         }*/
        //添加用户
        service.add(userForm);
        //保存成功信息

        req.setAttribute("code","success");
        req.setAttribute("msg","注册成功，请登录");
        return "f:/jsps/msg.jsp";
    }

    public Map<String,String> validata(User user,HttpSession session)
    {
        Map<String,String> map=new HashMap<String,String>();
        //校验用户名
        if (user.getLoginname()==null||user.getLoginname().trim().equals(""))
        {
            map.put("loginname","用户名为空");
        }
        else if (user.getLoginname().length()<3||user.getLoginname().length()>20)
        {
            map.put("loginname","用户名长度必须在3到20之间");
        }
        else if (!service.AJAXcheckUserName(user.getLoginname()))
        {
            map.put("loginname","用户名已经存在");
        }
        //校验密码
        if (user.getLoginpass()==null||user.getLoginpass().trim().equals(""))
        {
            map.put("loginpass","密码为空");
        }
        else if (user.getLoginpass().length()>3||user.getLoginpass().length()>20)
        {
            map.put("loginpass","密码长度必须在3到20之间");
        }
        //确认密码校验
        if (user.getReloginpass()==null||user.getReloginpass().trim().equals(""))
        {
            map.put("reloginpass","密码为空");
        }
        else if (!user.getReloginpass().equals(user.getLoginpass()))
        {
            map.put("reloginpass","两次密码输入相同");
        }
        //校验邮箱
        if (user.getEmail()==null||user.getEmail().trim().equals(""))
        {
            map.put("email","邮箱为空");
        }
        else  if (!user.getEmail().matches("^[a-z0-9]+([._\\\\-]*[a-z0-9])*@([a-z0-9]+[-a-z0-9]*[a-z0-9]+.){1,63}[a-z0-9]+$"))
        {
            map.put("email","邮箱格式错误");
        }
        else if (!service.AJAXcheckEmail(user.getEmail()))
        {
            map.put("email","邮箱已经存在");
        }
        //校验验证码
        if (user.getVerifycode()==null||user.getVerifycode().trim().equals(""))
        {
            map.put("verifycode","验证码为空");
        }
        else if(user.getVerifycode().length()>4||user.getVerifycode().length()>4)
        {
            map.put("verifycode","验证码长度错误");
        }
        else if (!user.getVerifycode().equals((String)session.getAttribute("vCode")))
        {
            map.put("verifycode","验证码不匹配");
        }
        return  map;
    }
}
